﻿@using DevFramework.Entities.Concrete
@model List<Product>

@{
    ViewBag.Title = "title";
    Layout = "~/Areas/Admin/Views/_LayoutAdmin.cshtml";
}
@(Html.Kendo().Grid<Product>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(p => p.ProductName);
        columns.Bound(t=>t.CategoryId).EditorTemplateName("Categories").Width(350);
        //columns.ForeignKey(p => p.CategoryId, (System.Collections.IEnumerable)ViewData["kategoriler"], "CategoryId", "CategoryName").Width(250)
        //    .Title("Category").Width(150);
        columns.Bound(p => p.UnitPrice).Width(150);

        //columns.Bound(c => c.Date).HtmlAttributes(new
        //{
        //    @class = "templateCell"

        //}).ClientTemplate(
        // Html.Kendo().DatePicker()
        //  .Name("FDPicker_#=ProductId#")
        //  .Format("{0:dd/MM/yyyy}")
        //  .HtmlAttributes(new { data_bind = "value:Date" })
        //  .ToClientTemplate().ToString()

        //  ).Format("{0:dd/MM/yyyy}");
    })
    .ToolBar(toolBar => toolBar.Save())
    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Pageable()
    //.Events(ev => ev.DataBound("db"))
    .DataSource(dataSource => dataSource
        .Ajax()
        .Batch(true)
        .PageSize(20)
        .ServerOperation(false)
        .Events(events => events.Error("errorHandler"))
        .Model(model =>
        {
            model.Id(p => p.ProductId);
            model.Field(p => p.ProductId).Editable(false);
            model.Field(p => p.ProductName).Editable(false);
            model.Field(p => p.UnitPrice).Editable(false);
            //model.Field(p => p.Date).Editable(false);
            model.Field(p => p.CategoryId);
        })
        .Read(read => read.Action("ForeignKeyColumn_Read", "Product",new{area="Admin"}))
        .Update(update => update.Action("UpdatePersons", "Product", new { area = "Admin" })))
    )

<script type="text/javascript">
    function errorHandler(e) {
        if (e.errors) {
            var message = "Errors:\n";
            $.each(e.errors, function (key, value) {
                if ('errors' in value) {
                    $.each(value.errors, function () {
                        message += this + "\n";
                    });
                }
            });
            alert(message);
        }
    }
</script>

<script type="text/javascript">
    function db(e) {
        var grid = this;
        $(".templateCell").each(function () {
            eval($(this).children("script").last().html());
            var tr = $(this).closest('tr');
            var item = grid.dataItem(tr);
            kendo.bind($(this), item);
        });
    }
</script>

